import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import request from 'utils/request';
import { CPAAS_PLATFORM } from 'utils/config';
import { parseParameters, isTenantRoleLevel, getCurrentOrganizationId } from 'utils/utils';
var tenantId = getCurrentOrganizationId();
var isSiteFlag = !isTenantRoleLevel();

function sourceApi() {
  return isTenantRoleLevel() ? "".concat(tenantId, "/datasources") : "datasources";
}

function serviceApi() {
  return isSiteFlag ? "datasource-services" : "".concat(tenantId, "/datasource-services");
}
/**
 * 查询数据源列表数据
 * @async
 * @function fetchDataSourceList
 * @param {object} params - 查询条件
 * @param {!number} [params.page = 0] - 数据页码
 * @param {!number} [params.size = 10] - 分页大小
 * @returns {object} fetch Promise
 */


export function fetchDataSourceList(_x) {
  return _fetchDataSourceList.apply(this, arguments);
}
/**
 * 查询数据源详情
 * @async
 * @function fetchDataSourceDetail
 * @param {object} params - 查询条件
 * @returns {object} fetch Promise
 */

function _fetchDataSourceList() {
  _fetchDataSourceList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(params) {
    var param;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            param = parseParameters(params);
            return _context.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(sourceApi()), {
              method: 'GET',
              query: param
            }));

          case 2:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _fetchDataSourceList.apply(this, arguments);
}

export function fetchDataSourceDetail(_x2) {
  return _fetchDataSourceDetail.apply(this, arguments);
}
/**
 * 添加数据源信息
 * @async
 * @function createDataSource
 * @param {object} params - 请求参数
 * @param {!object} params.datasourceCode - 数据源编码
 * @param {!string} params.description - 描述
 * @param {?number} params.datasourceUrl - URL地址
 * @param {!string} params.user - 用户
 * @param {?number} params.password - 密码
 * @param {!string} params.enabledFlag - 启用标记
 * @returns {object} fetch Promise
 */

function _fetchDataSourceDetail() {
  _fetchDataSourceDetail = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(params) {
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            return _context2.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(sourceApi(), "/").concat(params.datasourceId), {
              method: 'GET',
              query: params
            }));

          case 1:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _fetchDataSourceDetail.apply(this, arguments);
}

export function createDataSource(_x3) {
  return _createDataSource.apply(this, arguments);
}
/**
 * 编辑数据源信息
 * @async
 * @function editDataSource
 * @param {object} params - 请求参数
 * @param {!object} params.datasourceCode - 数据源编码
 * @param {!string} params.description - 描述
 * @param {?number} params.datasourceUrl - URL地址
 * @param {!string} params.user - 用户
 * @param {?number} params.password - 密码
 * @param {!string} params.enabledFlag - 启用标记
 * @param {!string} params.datasourceId - datasourceId
 * @param {!string} params.objectVersionNumber - 版本号
 * @returns {object} fetch Promise
 */

function _createDataSource() {
  _createDataSource = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(params) {
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            return _context3.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(sourceApi()), {
              method: 'POST',
              body: params
            }));

          case 1:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _createDataSource.apply(this, arguments);
}

export function editDataSource(_x4) {
  return _editDataSource.apply(this, arguments);
} // /**
//  * 删除数据源信息
//  * @async
//  * @function deleteDataSource
//  * @param {number} datasourceId - 数据源Id
//  * @returns {object} fetch Promise
//  */
// export async function deleteDataSource(params) {
//   return request(`${CPAAS_PLATFORM}/v1/${sourceApi()}`, {
//     method: 'DELETE',
//     body: params,
//   });
// }

/**
 * todo 废弃，保存的同时测试
 * 测试数据源
 * @async
 * @function createDataSource
 * @param {object} params - 请求参数
 * @param {!object} params.datasourceCode - 数据源编码
 * @param {!string} params.description - 描述
 * @param {?number} params.datasourceUrl - URL地址
 * @param {!string} params.user - 用户
 * @param {?number} params.password - 密码
 * @param {!string} params.enabledFlag - 启用标记
 * @returns {object} fetch Promise
 */

function _editDataSource() {
  _editDataSource = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(params) {
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            return _context4.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(sourceApi()), {
              method: 'PUT',
              body: params
            }));

          case 1:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _editDataSource.apply(this, arguments);
}

export function handleTestDataSource(_x5) {
  return _handleTestDataSource.apply(this, arguments);
}
/**
 * 获取连接池参数
 * @async
 * @function getDbPoolParams
 * @param {object} params - 查询条件
 * @returns {object} fetch Promise
 */

function _handleTestDataSource() {
  _handleTestDataSource = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(params) {
    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            return _context5.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/datasources/check"), {
              method: 'GET',
              query: _objectSpread({}, params)
            }));

          case 1:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  }));
  return _handleTestDataSource.apply(this, arguments);
}

export function getDbPoolParams(_x6) {
  return _getDbPoolParams.apply(this, arguments);
}
/**
 * 获取驱动类
 * @async
 * @function getDriverClass
 * @param {object} params - 查询条件
 * @returns {object} fetch Promise
 */

function _getDbPoolParams() {
  _getDbPoolParams = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(params) {
    return _regeneratorRuntime.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            return _context6.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(sourceApi(), "/").concat(params.dbPoolType, "/dbpool-option"), {
              method: 'GET'
            }));

          case 1:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  }));
  return _getDbPoolParams.apply(this, arguments);
}

export function getDriverClass(_x7) {
  return _getDriverClass.apply(this, arguments);
}
/**
 * 查询服务
 * @async
 * @function fetchServiceList
 * @param {object} params - 查询条件
 * @param {!number} [params.page = 0] - 数据页码
 * @param {!number} [params.size = 10] - 分页大小
 * @returns {object} fetch Promise
 */

function _getDriverClass() {
  _getDriverClass = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(params) {
    return _regeneratorRuntime.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            return _context7.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(sourceApi(), "/").concat(params.dbType, "/initialize"), {
              method: 'GET'
            }));

          case 1:
          case "end":
            return _context7.stop();
        }
      }
    }, _callee7);
  }));
  return _getDriverClass.apply(this, arguments);
}

export function fetchServiceList(_x8) {
  return _fetchServiceList.apply(this, arguments);
}
/**
 * 添加服务
 * @async
 * @function addService
 * @param {object} params - 请求参数
 * @param {!object} params.datasourceId - 数据库id
 * @param {!string} params.tenantId - 租户id
 * @returns {object} fetch Promise
 */

function _fetchServiceList() {
  _fetchServiceList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8(params) {
    return _regeneratorRuntime.wrap(function _callee8$(_context8) {
      while (1) {
        switch (_context8.prev = _context8.next) {
          case 0:
            return _context8.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(serviceApi()), {
              method: 'GET',
              query: params
            }));

          case 1:
          case "end":
            return _context8.stop();
        }
      }
    }, _callee8);
  }));
  return _fetchServiceList.apply(this, arguments);
}

export function addService(_x9) {
  return _addService.apply(this, arguments);
}
/**
 * 删除服务
 * @async
 * @function deleteService
 * @param {object} params,datasourceId - 请求参数
 * @param {number} datasourceServiceId - datasourceServiceId
 * @returns {object} fetch Promise
 */

function _addService() {
  _addService = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9(params) {
    return _regeneratorRuntime.wrap(function _callee9$(_context9) {
      while (1) {
        switch (_context9.prev = _context9.next) {
          case 0:
            return _context9.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(serviceApi()), {
              method: 'POST',
              body: params
            }));

          case 1:
          case "end":
            return _context9.stop();
        }
      }
    }, _callee9);
  }));
  return _addService.apply(this, arguments);
}

export function deleteService(_x10) {
  return _deleteService.apply(this, arguments);
}
/**
 * 表单配置
 * @async
 * @function fetchFormParams
 * @param {object} params - 请求参数
 * @returns {object} fetch Promise
 */

function _deleteService() {
  _deleteService = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee10(params) {
    return _regeneratorRuntime.wrap(function _callee10$(_context10) {
      while (1) {
        switch (_context10.prev = _context10.next) {
          case 0:
            return _context10.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(serviceApi()), {
              method: 'DELETE',
              body: params
            }));

          case 1:
          case "end":
            return _context10.stop();
        }
      }
    }, _callee10);
  }));
  return _deleteService.apply(this, arguments);
}

export function fetchFormParams(_x11) {
  return _fetchFormParams.apply(this, arguments);
}
/**
 * 删除服务驱动
 * @param params
 * @returns {Promise<Object>}
 */

function _fetchFormParams() {
  _fetchFormParams = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee11(params) {
    return _regeneratorRuntime.wrap(function _callee11$(_context11) {
      while (1) {
        switch (_context11.prev = _context11.next) {
          case 0:
            return _context11.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1").concat(isSiteFlag ? '/' : "/".concat(tenantId, "/"), "form-lines/header-code"), {
              method: 'GET',
              query: params
            }));

          case 1:
          case "end":
            return _context11.stop();
        }
      }
    }, _callee11);
  }));
  return _fetchFormParams.apply(this, arguments);
}

export function testConnection() {
  return _testConnection.apply(this, arguments);
}

function _testConnection() {
  _testConnection = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee12() {
    var params,
        _args12 = arguments;
    return _regeneratorRuntime.wrap(function _callee12$(_context12) {
      while (1) {
        switch (_context12.prev = _context12.next) {
          case 0:
            params = _args12.length > 0 && _args12[0] !== undefined ? _args12[0] : {};
            return _context12.abrupt("return", request("".concat(CPAAS_PLATFORM, "/v1/").concat(isSiteFlag ? '' : "".concat(tenantId, "/"), "datasources/test-connection"), {
              method: 'POST',
              body: params
            }));

          case 2:
          case "end":
            return _context12.stop();
        }
      }
    }, _callee12);
  }));
  return _testConnection.apply(this, arguments);
}